package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.gw.service.TrafficService;
import com.adcc.ags.gms.gw.view.ExcelView;
import com.adcc.ags.gms.gw.vo.TrafficVO;
import com.adcc.ags.gms.user.service.GWUserService;
import com.adcc.ags.gms.user.vo.GWUserVO;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuechao on 2016/9/2.
 */
@Controller
@RequestMapping(value="/traffic",produces = MediaType.APPLICATION_JSON_VALUE)
public class TrafficController {
    @Autowired
    private TrafficService trafficService;

    @Autowired
    private GWUserService gwUserService;

    @Autowired
    private LogFactory log;

    @RequestMapping(value = "/getTrafficsByCondition", method = RequestMethod.GET)
    public @ResponseBody
    ResultVO getTrafficsByCondition(String rawMsg,String state,String dateStart, String dateEnd,String sender, String receiver, String msgType, int currentPage, int pageSize){
        ResultVO resultVO = new ResultVO();
        try {
            Optional<PageInfo<TrafficVO>> list = trafficService.findAll(rawMsg, state, dateStart, dateEnd, sender, receiver, msgType,currentPage,pageSize);
            if(list.isPresent()){
                resultVO.setPageInfo(list.get());
                if(list.get().getList().size()>0){
                    resultVO.getData().put("TrafficVO", list.get().getList());
                }
            }else{
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_DB_ERROR);
            }
            return resultVO;
        } catch (Exception e) {
            log.error(TrafficController.class, "getTrafficsByCondition() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
            return resultVO;
        }
    }

    /**
     * 报文发送方、接收方下拉框选项
     * @return
     */
    @RequestMapping(value = "/getUsersByCondition", method = RequestMethod.GET)
    public @ResponseBody ResultVO getUsersByCondition(){
        ResultVO resultVO = new ResultVO();
        try{
            Optional<List<GWUserVO>> optional = gwUserService.findAllGWUser();
            if(optional.isPresent()){
                if(optional.get().size()>0){
                    resultVO.getData().put("GWUserVO",optional.get());
                }
            }else{
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_DB_ERROR);
            }
            return resultVO;
        } catch (Exception e) {
            log.error(TrafficController.class, "getUsersByCondition() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
            return resultVO;
        }
    }
    @RequestMapping(value = "exportExcel")
    public ModelAndView exportExcel(String rawMsg,String state,String dateStart, String dateEnd,String sender, String receiver, String msgType,HttpServletResponse response){
        Map<String,List<TrafficVO>> map = new HashMap<String, List<TrafficVO>>();
        ExcelView ve = new ExcelView();
        try {
            Optional<PageInfo<TrafficVO>> list = trafficService.findAll(rawMsg, state, dateStart, dateEnd, sender, receiver, msgType,null,null);
            if(list.isPresent() && list.get().getList().size()>0) {
                List<TrafficVO> result = list.get().getList();
                map.put("trafficMsg", result);
            }
        } catch (Exception e){
            log.error(TrafficController.class, "exportExcel error!", e);
        }
        return new ModelAndView(ve,map);
    }
}